package cn.iocoder.yudao.module.design.dal.mysql.problem;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.design.controller.admin.problem.vo.ProblemExportReqVO;
import cn.iocoder.yudao.module.design.controller.admin.problem.vo.ProblemPageReqVO;
import cn.iocoder.yudao.module.design.dal.dataobject.problem.ProblemCountDO;
import cn.iocoder.yudao.module.design.dal.dataobject.problem.ProblemDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Collection;
import java.util.List;

/**
 * 项目问题 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ProblemMapper extends BaseMapperX<ProblemDO> {

    default PageResult<ProblemDO> selectPage(ProblemPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProblemDO>()
                .likeIfPresent(ProblemDO::getTitle, reqVO.getTitle())
                .eqIfPresent(ProblemDO::getHouseId, reqVO.getHouseId())
                .eqIfPresent(ProblemDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ProblemDO::getDealerId, reqVO.getDealerId())
                .eqIfPresent(ProblemDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ProblemDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ProblemDO::getRemark, reqVO.getRemark())
                .eqIfPresent(ProblemDO::getFlowId, reqVO.getFlowId())
                .eqIfPresent(ProblemDO::getNodeId, reqVO.getNodeId())
                .eqIfPresent(ProblemDO::getProblemLevel, reqVO.getProblemLevel())
                .betweenIfPresent(ProblemDO::getExFinishTime, reqVO.getExFinishTime())
                .betweenIfPresent(ProblemDO::getFinishTime, reqVO.getFinishTime())
                .orderByAsc(ProblemDO::getStatus)
                .orderByAsc(ProblemDO::getUpdateTime));
    }

    default PageResult<ProblemDO> selectPageInIds(ProblemPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProblemDO>()
                .inIfPresent(ProblemDO::getId, reqVO.getIds())
                .likeIfPresent(ProblemDO::getTitle, reqVO.getTitle())
                .eqIfPresent(ProblemDO::getHouseId, reqVO.getHouseId())
                .eqIfPresent(ProblemDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ProblemDO::getDealerId, reqVO.getDealerId())
                .eqIfPresent(ProblemDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ProblemDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ProblemDO::getRemark, reqVO.getRemark())
                .eqIfPresent(ProblemDO::getFlowId, reqVO.getFlowId())
                .eqIfPresent(ProblemDO::getNodeId, reqVO.getNodeId())
                .eqIfPresent(ProblemDO::getProblemLevel, reqVO.getProblemLevel())
                .betweenIfPresent(ProblemDO::getExFinishTime, reqVO.getExFinishTime())
                .betweenIfPresent(ProblemDO::getFinishTime, reqVO.getFinishTime())
                .orderByAsc(ProblemDO::getStatus)
                .orderByAsc(ProblemDO::getUpdateTime));
    }

    default List<ProblemDO> selectList(ProblemExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ProblemDO>()
                .eqIfPresent(ProblemDO::getHouseId, reqVO.getHouseId())
                .eqIfPresent(ProblemDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ProblemDO::getDealerId, reqVO.getDealerId())
                .eqIfPresent(ProblemDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(ProblemDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ProblemDO::getRemark, reqVO.getRemark())
                .eqIfPresent(ProblemDO::getFlowId, reqVO.getFlowId())
                .eqIfPresent(ProblemDO::getNodeId, reqVO.getNodeId())
                .likeIfPresent(ProblemDO::getTitle, reqVO.getTitle())
                .eqIfPresent(ProblemDO::getProblemLevel, reqVO.getProblemLevel())
                .betweenIfPresent(ProblemDO::getExFinishTime, reqVO.getExFinishTime())
                .betweenIfPresent(ProblemDO::getFinishTime, reqVO.getFinishTime())
                .orderByAsc(ProblemDO::getStatus)
                .orderByAsc(ProblemDO::getUpdateTime));
    }

    @Select(value="select count(1) from hd_problem where ( (user_id=#{userId} and status =0) or ( dealer_id=#{userId}  and status  = 1 ) ) and house_id =#{houseId} ")
    int selectProblemCount(@Param("houseId")Long houseId, @Param("userId")Long userId);

    int selectHouseUserProblemCount(@Param("houseId")Long houseId, @Param("userId")Long userId);
    List<ProblemCountDO> selectProblemCountList(@Param("houseIds")Collection<Long> houseIds , @Param("userId")Long userId);
//    int selectProblemCountInHouseIds(@Param("houseId")Long houseId, @Param("userId")Long userId);

    List<ProblemDO> getRelProblemList(@Param("problemId") Long problemId, @Param("statusCd") String statusCd);

    void updateRelHandUpStatus(@Param("problemId") Long problemId, @Param("statusCd") String statusCd);

    List<ProblemDO> getRelUnProblemList(Long problemId);
    /** 
     * 查询未关联的问题（自己创建，状态为待处理  没有被关联的）
     * @author Annie
     * @time 2023/8/1 16:24
     * @param[1] problemId
     * @param[2] houseId
     * @param[3] userId
     * @return List<ProblemDO>
     * @throws
     */

    List<ProblemDO> getUnRelProblemList(@Param("problemId") Long problemId,@Param("houseId") Long houseId,@Param("userId") Long userId);
    /**
     * TODO
     *  查询问题 依赖的前置问题
     * @author Administrator
     * @time 2023/8/1 23:46
     * @param[1] problemId
     * @return List<ProblemDO>
     * @throws
     */
    List<ProblemDO>  getPreRelProblemList(@Param("problemId") Long problemId,@Param("title") String title);
}
